﻿using System;
using System.IO;
using System.Text;

namespace ClientWPF
{
    public class Log
    {
        private readonly FileStream fileStream;
        private readonly StreamWriter streamWriter;

        public Log()
        {
            fileStream = new FileStream("errorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            streamWriter = new StreamWriter(fileStream);
        }

        public void WriteToLog(Exception exception)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("Exception message:\n");
            stringBuilder.Append(exception.Message);
            stringBuilder.Append("\n");
            if (exception.InnerException != null)
            {
                stringBuilder.Append("Inner Exception message:\n");
                stringBuilder.Append(exception.InnerException.Message);
                stringBuilder.Append("\n");
            }
            if (!String.IsNullOrEmpty(exception.StackTrace))
            {
                stringBuilder.Append("Stack Trace:\n");
                stringBuilder.Append(exception.StackTrace);
                stringBuilder.Append("\n");
            }
            streamWriter.Write(stringBuilder);
            streamWriter.Flush();
            //fileStream.Write(stringBuilder, 0, stringBuilder.Length);
        }
    }
}